package main

import (
	"cmt1-admin/conf"
	"cmt1-admin/controllers"
	"cmt1-admin/routers"
	"math/rand"

	"flag"
	"fmt"
	"log"
	"net/http"
	"os"
	"time"
)

type params struct {
	*flag.FlagSet `json:"-"`
	appConf       string
	help          bool
}

func main() {
	cmdParams := &params{}
	cmdParams.FlagSet = flag.NewFlagSet("Cmt1 Server Admin Params", flag.ContinueOnError)
	cmdParams.StringVar(&cmdParams.appConf, "app-conf", "appconf/app.conf", "app.conf 配置文件")
	cmdParams.BoolVar(&cmdParams.help, "h", false, "help")
	err := cmdParams.Parse(os.Args[1:])
	if err != nil {
		os.Exit(0)
	}

	if cmdParams.help {
		cmdParams.Usage()
		return
	}

	err = conf.Conf.Load(cmdParams.appConf)
	if err != nil {
		log.Fatal("conf load err:", err)
	}
	controllers.InitDB(conf.Conf)

	rand.Seed(time.Now().UnixNano())

	r := routers.Router()
	s := &http.Server{
		Addr:           ":" + conf.Conf.Port,
		Handler:        r,
		ReadTimeout:    120 * time.Second,
		WriteTimeout:   120 * time.Second,
		MaxHeaderBytes: 1 << 20,
	}
	fmt.Println("start admin, listening ", conf.Conf.Port, " ...")
	s.ListenAndServe()
}
